<template>
	<div class="paper-list">
		<el-form :inline="true" :model="dataForm" @keyup.enter.native="getListDate()">
			<el-form-item>
				<el-input v-model="dataForm.title" placeholder="试卷名称" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-input v-model="dataForm.courseName" placeholder="课程名称" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="getListDate()">查询</el-button>
				<el-button v-if="isAuth('chapter:save')" type="primary" @click="saveHandle()">新增</el-button>
				<!-- <el-button v-if="isAuth('chapter:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
			</el-form-item>
		</el-form>
		<el-table :data="dataList" border style="width: 100%">
			<el-table-column prop="id" label="试卷id" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="title" label="试卷名称" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="courseName" label="所属课程" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="duration" label="时长(分钟)" width="100px" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="topicNum" label="题目数量" width="100px" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="createName" label="创建人" width="180px" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="120px" header-align="center" align="center">
			</el-table-column>
			<el-table-column prop="status" label="状态" width="100px" header-align="center" align="center">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.status === 0" size="small" type="warning">停用</el-tag>
					<el-tag v-if="scope.row.status === 1" size="small">正常</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="type" label="组卷类型" width="100px" header-align="center" align="center">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.type === 0" size="small" type="warning">未组卷</el-tag>
					<el-tag v-if="scope.row.type === 1" size="small">自动组卷</el-tag>
					<el-tag v-if="scope.row.type === 2" size="small">老师组卷</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="testMode" label="考试类型" width="100px" header-align="center" align="center">
				<template slot-scope="scope">
					<el-tag v-if="scope.row.testMode === 1" size="small" type="warning">练习</el-tag>
					<el-tag v-if="scope.row.testMode === 2" size="small">考试</el-tag>
				</template>
			</el-table-column>
			<el-table-column fixed="right" label="操作" header-align="center" align="center">
				<template slot-scope="scope">
					<el-button v-if="isAuth('testPaper:save')" @click="saveHandle(scope.row.id)" type="text"
						size="small">编辑
					</el-button>
					<el-button v-if="isAuth('testPaper:status:modify')" @click="deleteHandle(scope.row.id)" type="text"
						size="small">删除</el-button>
					<el-button v-if="isAuth('testPaper:topic:modify')" @click="autoPaper(scope.row)"
						style="color: #00A0E9;" size="small">自动组卷</el-button>
					<el-button v-if="isAuth('testPaper:topic:modify')" @click="manualPaper(scope.row)"
						style="color: #00A0E9;" size="small">手动组卷</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
			:page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
			layout="total, sizes, prev, pager, next, jumper">
		</el-pagination>
		<!-- 弹窗, 新增 / 修改 -->
		<save v-if="addOrUpdateVisible" ref="save" @refreshDataList="getListData"></save>
		<!-- 弹窗，自动组卷 -->
		<auto-dialog ref="autoDialog" v-if="addOrUpdateVisible"></auto-dialog>
		<!-- 弹窗，手动组卷 -->
		<manual-dialog ref="manualDialog" v-if="addOrUpdateVisible"></manual-dialog>
		<!-- 弹窗，手动组卷 -->
		<paperInfo-dialog ref="paperInfoDialog" v-if="addOrUpdateVisible"></paperInfo-dialog>
	</div>
</template>

<script>
	import Save from './paper-save'
	import autoDialog from './auto-dialog.vue'
	import manualDialog from './manual-dialog.vue'
	import paperInfoDialog from './paperInfo-dialog.vue'

	export default {
		data() {
			return {
				dataForm: {
					name: '',
					courseName: ''
				},
				dataList: [],
				pageIndex: 1,
				pageSize: 10,
				totalPage: 0,
				dataListLoading: false,
				dataListSelections: [],
				addOrUpdateVisible: false
			}
		},
		components: {
			Save,
			autoDialog,
			manualDialog,
			paperInfoDialog
		},

		activated() {
			this.getListDate();
		},
		methods: {
			getListDate() {
				let _that = this;
				this.dataListLoading = true
				this.$http({
					url: this.$http.adornUrl('/testPaper/list'),
					method: 'get',
					params: this.$http.adornParams({
						'page': this.pageIndex,
						'limit': this.pageSize,
						'courseName': this.dataForm.courseName,
						'title': this.dataForm.title
					})
				}).then(({
					data
				}) => {
					_that.dataList = []
					if (data && data.code === 0) {
						_that.dataList = data.data
						_that.totalPage = data.count
					} else {
						_that.dataList = []
						_that.totalPage = 0
					}
					this.dataListLoading = false
				})
			},
			// 每页数
			sizeChangeHandle(val) {
				this.pageSize = val
				this.pageIndex = 1
				this.getListDate()
			},
			// 当前页
			currentChangeHandle(val) {
				this.pageIndex = val
				this.getListDate()
			},
			// 多选
			selectionChangeHandle(val) {
				this.dataListSelections = val
			},
			// 新增 / 修改
			saveHandle(id) {
				this.addOrUpdateVisible = true
				this.$nextTick(() => {
					this.$refs.save.init(id)
				})
			},


			// 删除
			deleteHandle(id) {
				var userIds = id ? [id] : this.dataListSelections.map(item => {
					return item.userId
				})
				this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					var req = {
						"ids": userIds,
						"status": -1
					};
					this.$http({
						url: this.$http.adornUrl('/testPaper/status/modify'),
						method: 'post',
						// params: this.$http.adornParams({
						//  "req":req
						// })
						data: this.$http.adornData({
							"req": req
						})
					}).then(({
						data
					}) => {
						if (data && data.code === 0) {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.getDataList()
								}
							})
						} else {
							this.$message.error(data.msg)
						}
					})
				}).catch(() => {})
			},
			/**
			 * 自动组卷
			 */
			autoPaper(row) {
				this.addOrUpdateVisible = true
				this.$nextTick(() => {
					this.$refs.autoDialog.init(row.id, row.courseId, row.topicNum, row.title);
				})
			},
			/**
			 * 手动组卷
			 */
			manualPaper(row) {
				this.addOrUpdateVisible = true
				this.$nextTick(() => {
					this.$refs.manualDialog.init(row.id, row.courseId, row.topicNum, row.title);
				})
			},
			/**
			 * @param {Object} row
			 * 查看试卷
			 */
			getPaper(paperId, paperNum, paperTitle) {
				console.log("执行了");
				this.addOrUpdateVisible = true;
				this.$nextTick(() => {
					this.$refs.paperInfoDialog.init(paperId, paperNum, paperTitle);
				})
			}
		}
	}
</script>
